/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include <stdarg.h>
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLAction.h"


//***********************************************
//** Module Elements
//***********************************************

//***********************************************
//** Exported Functions
//***********************************************
MDLLFUN bool CGLActionPrint(int meshid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLMeshInfo *meshinfo=GlobalMeshList.Find(meshid);
	if(meshinfo==NULL)
		{
		MStdPrintf("Unable to find mesh %d\n",meshid);
		return false;
		}

	IAnimatedMesh *imesh=meshinfo->GetMesh();
	if(imesh->getMeshType()!=EAMT_MD2)
		{
		MStdPrintf("**Mesh %d is not of MD2 type for Actions\n",meshid);
		return false;
		}

	IAnimatedMeshMD2 *imeshmd2=(IAnimatedMeshMD2 *)imesh;

	const int count=imeshmd2->getAnimationCount();

	if(count<=0)
		{
		MStdPrintf("**No action names found in Mesh %d\n",meshid);
		return false;
		}

	MStdPrintf("Action Info for Mesh %d (%d Actions,%d Frames)\n",meshid
			,(int)count ,(int)imeshmd2->getFrameCount() );

	int i;
	for(i=0;i<count;++i)
		{
		const char *animationname=(const char *)imeshmd2->getAnimationName(i);

		s32 loopbegin,loopend,fps;
		imeshmd2->getFrameLoop((c8 *)animationname,loopbegin,loopend,fps);

		// Now for each action get animation loop sequence
		MStdPrintf("\t%-20s  frame: begin=%3d end=%3d fps=%3d\n"
				,animationname,int(loopbegin),int(loopend),int(fps) );
		}

	return true;
	}


///////////////////////////////////////////////////////////
MDLLFUN bool CGLActionPlay(int nodeid,const char *action,bool loop)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(action==NULL)
		{
		action="run";
		}

	// Find Node
	MCGLNodeInfo *node=GlobalNodeList.Find(nodeid);
	if(node==NULL)
		{
		MStdPrintf("**Unable to find node with id=%d\n",nodeid);
		return false;
		}

	if(node->GetType()!=CGLNIT_ANIMATED_MESH)
		{
		MStdPrintf("**Node %d is not of an animated mesh type\n",nodeid);
		return false;
		}

	IAnimatedMeshSceneNode *inode=(IAnimatedMeshSceneNode *)node->GetNode();

	inode->setLoopMode(loop);
	
	if(inode->setMD2Animation(action)==false)
		{
		MStdPrintf("**Unable to set action %s to Node %d\n",action,nodeid);
		return false;
		}

	return true;
	}


///////////////////////////////////////////////////////////
MDLLFUN bool CGLActionSetSpeed(int nodeid,int fps)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// Find Node
	MCGLNodeInfo *node=GlobalNodeList.Find(nodeid);
	if(node==NULL)
		{
		MStdPrintf("**Unable to find node with id=%d\n",nodeid);
		return false;
		}

	if(node->GetType()!=CGLNIT_ANIMATED_MESH)
		{
		MStdPrintf("**Node %d is not of an animated mesh type\n",nodeid);
		return false;
		}
	
	IAnimatedMeshSceneNode *inode=(IAnimatedMeshSceneNode *)node->GetNode();
	inode->setAnimationSpeed(float(fps) );

	return true;
	}


